<template>
	<!-- 通用版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('public.cutCode')" />
		<zy-table :headList="headList" :dataList="dataList" border>
		</zy-table>
		<bottomBtn fixed :leftDisabled="disabled" :rightDisabled="disabled" :leftText="$t('public.reset')"
			:rightText="$t('homepage.CutMarket.instorage')" @rightBtn="submitBtn" @leftBtn="resetBtn"></bottomBtn>
		<!-- 储位扫描弹窗 -->
		<zy-popup ref="zyPopup" type='dialog' :title="$t('homepage.WarehouseManage.SPselect')" @cancel="cancel" @confirm="confirm" closeable
			:closeOnClickOverlay="false">
			<zy-forms :model="model" labelWidth="100">
				<zy-form-item :label="$t('public.storage')" required>
					<zy-input v-model="model.storage"
						:placeholder="$t('public.pleaseInput')+$t('public.storage')"
						@confirm="storageSearch" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanStorage"></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('public.position')" required>
					<zy-input v-model="model.position" @confirm="positionSearch"
						:placeholder="$t('public.pleaseInput')+$t('public.position')" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanPosition"></i>
					</zy-input>
				</zy-form-item>
			</zy-forms>
		</zy-popup>
		<!-- 提交二次确认 -->
		<zy-popup ref="popup" :title="$t('public.instorageSure')" type="dialog" @confirm="submitSecond">
			{{$t('homepage.CutMarket.isSureInstorage')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		positionGetEntityByCode,
		storageGetEntityByCode,
		saveCscWarehouse
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				disabled: true,
				model: {
					position: '',
					storage: '',
				},
				id: 0,
				//表格表头
				headList: [{
					name: this.$t('homepage.CutMarket.orderNum'),
					key: 'id',
				}, {
					name: this.$t('public.cutCode'),
					key: 'code',
				}],
				dataList: [], //表格数据
				barcode: "",
				positionId: '',
				storageId: '',
			}
		},
		// 监听表格是否有数据
		watch: {
			dataList: {
				handler(n, o) {
					if (n.length > 0) {
						this.disabled = false
					} else {
						this.disabled = true
					}
				},
				immediate: true,
				deep: true
			}
		},
		onLoad() {
			this.getscancodedate()
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
			uni.$off('scancodepopdate')
		},
		methods: {
			//主页获取箱号扫描事件
			getscancodedate() {
				// 移除监听“添加扫描”弹窗事件
				uni.$off('scancodepopdate')
				//PDA扫描主页指示单号
				var _this = this
				uni.$on('scancodedate', function(data) {
					_this.searchList(data.code)
				})
			},
			//“弹窗储位”获取扫描事件
			getscancodepopdate() {
				// 移除监听“指示单号”事件
				uni.$off('scancodedate')
				//PDA扫描“添加扫描”弹窗事件
				var _this = this
				uni.$on('scancodepopdate', function(data) {
					_this.model.storage = data.code
					_this.storageSearch()
				})
			},
			//裁片码
			searchList(e) {
				if (this.dataList.length == 0) {
					this.id++
					let obj = {}
					obj.id = this.id
					obj.code = e
					this.dataList.push(obj)
				} else {
					if (this.dataList.findIndex(item => item.code === e) !== -1) {
						uni.showToast({
							title: this.$t('public.searhRepeat'),
							icon: "none",
							duration: 3000
						})
					} else {
						this.id++
						let obj = {}
						obj.id = this.id
						obj.code = e
						this.dataList.push(obj)
					}
				}
			},
			//入库提交
			submitBtn() {
				this.$refs.zyPopup.open()
				this.getscancodepopdate()
			},
			//仓位扫描事件
			scanPosition() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.position = ret.resp_result;
							this.positionSearch()
						}
					})
			},
			//储位扫描事件
			scanStorage() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.storage = ret.resp_result;
							this.storageSearch()
						}
					})
			},
			//仓位接口
			positionSearch() {
				positionGetEntityByCode({
					code: this.model.position
				}).then((res) => {
					if (res.code == 0) {
						this.positionId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//储位接口
			storageSearch() {
				storageGetEntityByCode({
					code: this.model.storage
				}).then((res) => {
					if (res.code == 0) {
						this.model.position = res.data.warehousePosition.code
						this.positionSearch()
						this.storageId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 仓/储选择取消
			cancel() {
				this.model.position = ''
				this.model.storage = ''
				this.positionId = ''
				this.storageId = ''
			},
			// 页面重置
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/CutMarket/InStorage/index"
				})
			},
			// 二次确认事件
			confirm() {
				if (this.model.position && this.model.storage) {
					this.$refs.popup.open()
				} else {
					uni.showToast({
						title: this.$t('homepage.CutMarket.selectPositionAndStorage'),
						icon: "none",
						duration: 3000
					})
				}
			},
			//入库确认提交
			submitSecond() {
				for (var i = 0; i < this.dataList.length; i++) {
					this.barcode += this.dataList[i].code + ','
				}
				saveCscWarehouse({
					barcode: this.barcode,
					positionId: this.positionId,
					storageId: this.storageId
				}).then((res) => {
					if (res.code == 0) {
						this.resetBtn()
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
</style>